package com.bwie.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ProdDownTask {

    @Autowired
    TbProdMapper tbProdMapper;



    @Scheduled(cron = "0 0/1 * * * ?")
    public void prodDownTask(){
        log.info("开始执行商品下架任务");

        LambdaQueryWrapper<TbProd> wrapper = new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdStatus, 1).eq(TbProd::getDownType, 1).le(TbProd::getDownTime, new Date());

        List<TbProd> tbProds = tbProdMapper.selectList(wrapper);

        log.info("待下架商品数量：{}",tbProds.size());
        for (TbProd tbProd : tbProds) {
            tbProd.setProdStatus(0);
            tbProdMapper.updateById(tbProd);
        }
        log.info("商品下架任务执行完毕");
    }

}
